package com.aizhixin.lab.course.classes.service;

import lombok.extern.slf4j.Slf4j;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.core.UserInforCore;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.company.domain.AccountDomain;
import com.aizhixin.lab.company.domain.QueryUserDomain;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.entity.SystemSet;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.repository.SystemSetRepository;
import com.aizhixin.lab.course.classes.domain.QueryCourseClassDomain;
import com.aizhixin.lab.course.classes.entity.CourseClassStu;
import com.aizhixin.lab.course.classes.repository.CourseClassStuRepository;
import com.aizhixin.lab.task.domain.StuTaskInforDomain;

@Slf4j
@Service
@Transactional
public class CourseClassStuService {
	@Autowired
	private CourseClassStuRepository courseClassStuRepository;
	@Autowired
	private PageJdbcUtil pageJdbcUtil;
	@Autowired
	private RedisTemplate<String,String> redisTemplate;
	public void initCourseId(){
		courseClassStuRepository.initCourseId();
	}
	
	public void save(List<CourseClassStu> stuList) {
		courseClassStuRepository.save(stuList);
	}
	
	public long deleteByCourseClassId(String courseClassId) {
		return courseClassStuRepository.deleteByCourseClassId(courseClassId);
	}
	
	public void deleteByStuIds(List<Long> stuIds) {
		 courseClassStuRepository.updateByStuId(DataValidity.INVALID.getState(), stuIds);
	}
	
	
	 RowMapper<AccountDomain> rm2 = new RowMapper<AccountDomain>() {

			@Override
			public AccountDomain mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				// TODO Auto-generated method stub
				AccountDomain domain = new AccountDomain();
				domain.setId(rs.getLong("ID"));
				domain.setCollegeId(rs.getLong("COLLEGE_ID"));
				domain.setProfessionalId(rs.getLong("PROFESSIONAL_ID"));
				domain.setJobNum(rs.getString("JOB_NUM"));
				domain.setSex(rs.getString("SEX"));
				domain.setCollegeName(rs.getString("COLLEGE_NAME"));
				domain.setProfessionalName(rs.getString("PROFESSIONAL_NAME"));
				domain.setName(rs.getString("NAME"));
				domain.setPhone(rs.getString("PHONE"));
				domain.setEmail(rs.getString("EMAIL"));
				domain.setClassId(rs.getLong("CLASS_ID"));
				domain.setClassName(rs.getString("CLASS_NAME"));
				domain.setDeleteFlag(rs.getInt("DELETE_FLAG"));
				domain.setUserName(rs.getString("user_name"));
				Set<String> keys = redisTemplate.keys(domain.getUserName()+":" + "*");
				if (keys.size()>0){
					domain.setIsRun(true);
				}else {
					domain.setIsRun(false);
				}
				return domain;
			}
		};
	    
	    public <T> Map<String, Object> findStudentPage(QueryCourseClassDomain domain) {

			String querySql = "SELECT U.ID,U.NAME,PHONE,u.user_name,EMAIL,JOB_NUM,SEX,c.NAME AS COLLEGE_NAME,p.NAME AS PROFESSIONAL_NAME,u.DELETE_FLAG,u.COLLEGE_ID,u.PROFESSIONAL_ID,u.CLASS_ID,cl.name as CLASS_NAME from `t_course_class_stu` ccs LEFT JOIN  `t_user` u  ON  u.id =ccs.stu_id left join `t_college` c on u.COLLEGE_ID = c.id LEFT JOIN `t_professional` p ON u.PROFESSIONAL_ID = p.ID left join `t_classes` cl on u.class_id =cl.id where ccs.delete_flag = 0 and ccs.course_class_id = '"+domain.getCourseClassId()+"' ";
			String countSql = "SELECT count(*) from `t_course_class_stu` ccs LEFT JOIN  `t_user` u  ON  u.id =ccs.stu_id where ccs.delete_flag = 0 and ccs.course_class_id = '"+domain.getCourseClassId()+"' ";
			if (!StringUtils.isEmpty(domain.getStuName())) {
				querySql += " and (u.NAME like '%" + domain.getStuName() + "%' OR u.JOB_NUM like '%" + domain.getStuName() + "%') ";
				countSql += " and (u.NAME like '%" + domain.getStuName() + "%' OR u.JOB_NUM like '%" + domain.getStuName() + "%') ";
			}
			List<SortDTO> sort = new ArrayList<SortDTO>();
			SortDTO dto = new SortDTO();
			dto = new SortDTO();
			dto.setKey("u.ID");
			dto.setAsc(true);
			sort.add(dto);
			log.debug("findStudentPage_sql--------->"+querySql);
			return pageJdbcUtil.getPageInfor(domain.getPageSize(),
					domain.getPageNumber(), rm2, sort, querySql, countSql);
		}
	    
	    public List<CourseClassStu> findAllByCourseClassIdIn(List<String> courseClassIds){
	    	return courseClassStuRepository.findAllByDeleteFlagAndCourseClassIdInOrderByCourseClassIdAsc(DataValidity.VALID.getState(), courseClassIds);
	    }
	    
	    
	    RowMapper<AccountDomain> rm = new RowMapper<AccountDomain>() {

			@Override
			public AccountDomain mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				// TODO Auto-generated method stub
				AccountDomain domain = new AccountDomain();
				domain.setId(rs.getLong("ID"));
				domain.setCollegeId(rs.getLong("COLLEGE_ID"));
				domain.setProfessionalId(rs.getLong("PROFESSIONAL_ID"));
				domain.setJobNum(rs.getString("JOB_NUM"));
				domain.setSex(rs.getString("SEX"));
				domain.setCollegeName(rs.getString("COLLEGE_NAME"));
				domain.setProfessionalName(rs.getString("PROFESSIONAL_NAME"));
				domain.setName(rs.getString("NAME"));
				domain.setPhone(rs.getString("PHONE"));
				domain.setEmail(rs.getString("EMAIL"));
				domain.setClassId(rs.getLong("CLASS_ID"));
				domain.setClassName(rs.getString("CLASS_NAME"));
				domain.setDeleteFlag(rs.getInt("DELETE_FLAG"));
				domain.setCourseClassId(rs.getString("course_class_id"));
				return domain;
			}
		};
	    
	    public List<AccountDomain> findAllByCourseClassIdIn(String courseClassIds) {

			String querySql = "SELECT U.ID,U.NAME,PHONE,EMAIL,JOB_NUM,SEX,c.NAME AS COLLEGE_NAME,p.NAME AS PROFESSIONAL_NAME,u.DELETE_FLAG,u.COLLEGE_ID,u.PROFESSIONAL_ID,u.CLASS_ID,cl.name as CLASS_NAME,ccs.course_class_id from `t_course_class_stu` ccs LEFT JOIN  `t_user` u  ON  u.id =ccs.stu_id left join `t_college` c on u.COLLEGE_ID = c.id LEFT JOIN `t_professional` p ON u.PROFESSIONAL_ID = p.ID left join `t_classes` cl on u.class_id =cl.id where ccs.delete_flag = 0 and ccs.course_class_id in ('"+courseClassIds+"') ";
			
			return pageJdbcUtil.getInfo(querySql, rm);
		}
}
